function ParameterValues = get_all_parameter_values(calibmethod, l)

% Christoph Gortz
% 18.04.2012
%
% This function is called by initialise_finite_horizon_decomp. It assigns
% parameter values.



%% Load results from Bayesian estimation exercise
% dynarefilename_restults.m has to be in the directory!
load estimation_twosbaggTFP48_rev2noFIdataNoMeallnews_results       % Contains posterior densities of estimated parameters
Postdens1 = oo_ .posterior_density.parameters;
Postdens2 = oo_ .posterior_density.shocks_std;
    
    
%% Load posterior density functions of the parameters and assign parameter values according to these functions

Estpar = fieldnames(Postdens1);  % returns a cell array of strings listing all fields belonging to Postdens


for i = 1:numel(Estpar)
    % Get the name of parameter i.
    paraname = deblank(Estpar(i,:)); % parameter name as cell
    parname = paraname{:};           % parameter name as string
    
    if calibmethod == 1
    % Assign a value to parameter i (according to posterior density)
    parafield = getfield(Postdens1,  parname);   % contains posterior density and associated parameter values
    rand = randperm(numel(parafield(:,2)));     % generates a random permutation of the integers from 1 to 512
    prod = rand'.*parafield(:,2);               % product of string of random integers with posterior density
    [a,ix] = max(prod);                         % find index of maximum of prod
    parvalue = parafield(ix,1);                 % get the value of parameter i.
    eval([ 'ParameterValues.e_' parname ' =  parvalue;']);      % Assign value to parameter name
    
    elseif calibmethod == 2
    Postmode = oo_ .posterior_mode.parameters;
    parafield = getfield(Postmode,  parname);   % contains posterior density and associated parameter values
    parvalue = parafield;                       % get the value of parameter i.
    eval([ 'ParameterValues.e_' parname ' =  parvalue;']); % Assign value 
                                                % to parameter name
    end
end

    
clear Estpar M_ a i ix options_ parafield paraname parname parvalue prod rand Postmode



%% Load posterior density functions of the shock std and assign parameter values according to these functions

Estsho = fieldnames(Postdens2);  % returns a cell array of strings listing all fields belonging to Postdens
for i = 1:numel(Estsho)
    % Get the name of parameter i.
    shockname = deblank(Estsho(i,:)); % parameter name as cell
    shoname = shockname{:};           % parameter name as string
    
    if calibmethod == 1
    % Assign a value to parameter i (according to posterior density)
    parafield = getfield(Postdens2,  shoname);  % contains posterior density and associated parameter values
    rand = randperm(numel(parafield(:,2)));     % generates a random permutation of the integers from 1 to 512
    prod = rand'.*parafield(:,2);               % product of string of random integers with posterior density
    [a,ix] = max(prod);                         % find index of maximum of prod
    shovalue = parafield(ix,1);                 % get the value of parameter i.
    %  Only one shock std has its estimated value, others equal zero
    %if ns == i
       shovalue = parafield(ix,1);
    %else
    %  shovalue = 0;
    %end
    eval([ 'ParameterValues.std_' shoname ' =  shovalue;']);    % Assign value to parameter name
    
    elseif calibmethod == 2
    Postmode = oo_ .posterior_mode.shocks_std;
    parafield = getfield(Postmode,  shoname);   % contains posterior density and associated parameter values
    shovalue = parafield;                       % get the value of parameter i.
    %  Only one shock std has its estimated value, others equal zero
    %if ns == i
       shovalue = parafield;
    %else
    %   shovalue = 0;
    %end
    eval([ 'ParameterValues.std_' shoname ' =  shovalue;']);    % Assign value to parameter name
    end
   % All shock std's are called std_shoname to call them in the dynare file
end




clear Estsho M_ a i ix oo_ options_ parafield shockaname shoname shovalue prod rand Postmode Postdens1 Postdens2


end